# coding=utf-8

"""
    Created by Kalyter on 2017-11-21.
"""

from learning.scientifc import computer, plot
from learning.scientifc.readers import reader


class Strategy(object):
    """策略基类"""

    def run(self):
        """执行具体策略"""


def read_from_csv(filename='us.csv'):
    content = reader.read_csv(filename)
    titles = content.pop(0)
    titles.pop(0)
    data = []
    for row in content:
        row = row[1:]
        data_row = []
        for item in row:
            data_row.append(float(item))
        data.append(data_row)
    plot.simple_heat_map(data, titles, titles)


def sample_compare(step=5, max_comparision=None):
    """以TPP和TPPspanish样本作比较。


    :param step: 分词系数
    :param max_comparision: 最大需要比较的文件数，默认是所有文件
    """

    en_compare_file = './xml/TPP.xml'
    spanish_compare_file = './xml/TPPspanish.xml'

    a, en_compare = reader.read(en_compare_file)
    a, spanish_compare = reader.read(spanish_compare_file)
    data = []
    title = ('文件名', '相似度')
    for filename in reader.read_path('./xml', max_comparision):
        info, body = reader.read(filename)
        if not data:
            info_title = tuple(k for k, v in info.items())
            data.append(info_title + title)

        file_language = info.get('语言')

        if file_language == 'en':
            result = computer.compare_by_jaccard(en_compare, body, step)
        elif file_language == 'es':
            result = computer.compare_by_jaccard(spanish_compare, body, step)
        else:
            print('%s 没有对应比较对象' % file_language)
            result = -0.001

        print('%s   相似度   %.04f' % (filename, result))
        values = tuple(v for k, v in info.items())
        data.append(values + (filename, result))
    return data


def each_compare(step=5, max_comparision=None):
    """两两比较"""

    path_list = [i for i in reader.read_path('./China', max_comparision)]
    data = []
    tails = ('相似度', )
    for x in range(len(path_list) - 1):
        x_filename = path_list[x]
        x_info, x_file = reader.read(x_filename)
        if not data:
            compare_title = tuple('比较文件:' + k for k, v in x_info.items())
            compared_title = tuple('被比较文件:' + k for k, v in x_info.items())
            data.append(compare_title + compared_title + tails)

        for y in range(x + 1, len(path_list)):
            row = ()
            if y == x + 1:
                row += tuple(v for k, v in x_info.items())
            else:
                row += (None, ) * len(x_info)
            y_filename = path_list[y]
            y_info, y_file = reader.read(y_filename)
            result = computer.compare_by_jaccard(x_file, y_file, step)
            print('%s   %s  相似度     %.4f' % (x_filename, y_filename, result))
            row += tuple(v for k, v in y_info.items())
            data.append(row + (result, ))
    return data
